package com.payment.impl;

import java.util.List;
import java.util.Map;

import com.basic.service.contract.IBaseService;
import com.common.util.ListConvertMap;
import com.translate.entity.NoticeCenter;
import com.translate.entity.Order;
import com.translate.entity.User;
import com.translate.vo.ServiceTypeVO;
import com.translate.web.param.contract.IPayBehaviorRequestParam;
import com.translate.web.service.INoticeCenterService;
import com.translate.web.service.IOrderService;
import com.translate.web.service.IServiceTypeService;

public abstract class AbstractPay<T> {

	protected IBaseService<T>[] baseServices;

	protected interface IGetOrderCallback {

		public List<Order> apply(IPayBehaviorRequestParam behaviorRequestParam, User user);

	}

	IGetOrderCallback[] orderCallbacks = new IGetOrderCallback[] {

			(IPayBehaviorRequestParam behaviorRequestParam, User user) -> {
				IOrderService orderService = (IOrderService) baseServices[2];

				return orderService.getOrdersByUserIdAndManagerId(behaviorRequestParam);
			},

			(IPayBehaviorRequestParam behaviorRequestParam, User user) -> {
				IOrderService orderService = (IOrderService) baseServices[2];

				behaviorRequestParam.setUserId(user.getTeamId());

				return orderService.getOrdersByPayManagerIdAndTeamId(behaviorRequestParam.getId(), user.getTeamId());
			} };

	/**
	 * 消息通知
	 * 
	 * @param noticeCenterService
	 * @param serviceTypeService
	 * @param orderService
	 * @param behaviorRequestParam
	 * @return
	 */
	protected int generateNoticeData(INoticeCenterService noticeCenterService, IServiceTypeService serviceTypeService,
			List<Order> alreadyPaymentByBalance, User userData, String description) {

		List<ServiceTypeVO> listFromCache = serviceTypeService.getListFromCache(ServiceTypeVO.class);

		Map<Integer, ServiceTypeVO> listToMap = ListConvertMap.listToMap(listFromCache, ServiceTypeVO::getId);

//		int userId = (int) behaviorRequestParam.getUserId();

		int size = alreadyPaymentByBalance.size();

		NoticeCenter[] noticeCenters = new NoticeCenter[size];
		
		NoticeCenter center;
		
		Order order;
		
		int serviceId;
		
		ServiceTypeVO serviceTypeVO;
		
		NoticeCenter[] noticeAdmins = new NoticeCenter[size];
		
		NoticeCenter clone;
		
		String contentPrefix = 
				userData.getRealname().concat("(ID:") 
				+ userData.getId()+")提交【{TYPE_NAME}】订单{ORDER_SN},请及时查看处理!";
		
		for (int i = 0; i < size; i++) {
			
			order = alreadyPaymentByBalance.get(i);
			
			serviceId = order.getServiceId();
			
			serviceTypeVO = listToMap.get(serviceId);

			if (null == serviceTypeVO) {
				throw new IllegalArgumentException("添加通知时找不到服务id：" + serviceId);
			}
			
			center = generate(userData, order, description);

			String typeName = serviceTypeVO.getTypeName();
			String orderSnId = order.getOrderSnId();
			center.setNoticeContent("您的【" + typeName + "】订单" + orderSnId + description);
			
			noticeCenters[i] = center;
			
			try {
				clone = center.clone();
			} catch (CloneNotSupportedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				throw new IllegalStateException("失败");
			}
			
			contentPrefix = contentPrefix.replace("{TYPE_NAME}", typeName).replace("{ORDER_SN}", orderSnId);
			
			clone.setNoticeContent(contentPrefix);
			
			clone.setPlatform((byte) 1);
			
			noticeAdmins[i] = clone;
			
		}
		
		noticeCenterService.batchInsert(noticeCenters );
		
		noticeCenterService.batchInsert(noticeAdmins );
		
		return 1;
	
	}

	// 
	protected NoticeCenter generate(User userData, Order order, String description) {
		

		NoticeCenter center;

		long timeMillis = System.currentTimeMillis() / 1000;

		byte orderType = 0;
		
		int userId = userData.getId().intValue();

		
		center = new NoticeCenter();

		center.setUserId(userId);

		center.setTitle("提交订单");

		

		center.setCreateTime(timeMillis);

		center.setJump((byte) 0);

		center.setUpdateTime(timeMillis);
		
		center.setPlatform((byte) 0);

		center.setTypeId(order.getId());

		center.setNoticeType(orderType);
		
		return center;
	}

}
